package com.zhq.dao;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhq.Interface.user.dao.IUserDao;
import com.zhq.user.entity.User;


public class UserDaoTest {
	private IUserDao userDao;
	private static ClassPathXmlApplicationContext ctx;
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    	ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","com/zhq/user/user-beans.xml"});
    }
    
    
    @Before
    public void setUp() throws Exception {
    	userDao =(IUserDao)ctx.getBean("userDao");
    }
	
    @Test
	public void testSaveUser() {
    	User user = new User();
    	user.setUserName("zhq");
    	user.setPassword("zhq");
    	userDao.saveUser(user);
    }
	
	
	@Test
	public void testFetchUserById() {
		assertEquals(1, userDao.fetchUserById(1).getId());
	}
	
	@Test
	public void testFetchUsers() {
		System.out.println(userDao.fetchUsers());
	}
	
	@Test
	public void testUpdateUser() {
		User user  = userDao.fetchUserById(1);
		user.setUserName("111");
		userDao.updateUser(user);
		assertEquals("11", userDao.fetchUserById(1).getUserName());
	}
	
	//@Test
	public void testDeleteUser() {
		int id = 2;
		userDao.deleteUser(id);
	}
}
